<template>
    <div >
        <h3>当前求和为: {{sum}}</h3>
        <select v-model.number="num">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
        <button @click="increment">+</button>
        <button @click="decrement">-</button>
        <button @click="incrementOdd">当前求和为奇数再加</button>
        <button @click="incrementWait">等一等再加</button>
    </div>
</template>

<script>
export default {
  name: 'Count',
  data() {
      return {
        //当前求和
        sum: 0,
        //用户选择的数字
        num: 1
      }
  },
  methods: {
    //加1
    increment() {
      this.sum += this.num;
    },
    //减1
    decrement() {
      this.sum -= this.num;
    },
    //当前求和为奇数再加
    incrementOdd() {
      if (this.sum % 2 === 1) {
        this.sum += this.num;
      }
    },
    //等一等再加
    incrementWait() {
      setTimeout(() => {
        this.sum += this.num;
      }, 1000);
    } 
  }

}
</script>

<style scoped>
    button {
      margin: 4px;
    }
</style>